package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.User;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.bean.vo.FootprintListData;
import com.dropdb.demo.service.FootprintService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.Map;

@RestController
@RequestMapping("wx/footprint")
@Validated
public class WxFootprintController {

    @Autowired
    FootprintService footprintService;

    @GetMapping("list")
    public BaseRespVO list(@Min(1) Integer page, @Min(1) Integer size) {
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        FootprintListData footprintListData = footprintService.footprintList(user.getId(), page, size);
        return BaseRespVO.ok(footprintListData);
    }

    @PostMapping("delete")
    public BaseRespVO delete(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        if (null == id) {
            return BaseRespVO.error(402, "参数非法");
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        int i = footprintService.deleteFootprint(user.getId(), id);
        return 1 == i ? BaseRespVO.ok() : BaseRespVO.error(502, "系统内部错误");
    }
}
